package com.wxtech.heartbeat;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.wxtech.heartbeat.database.DataManager;
import com.wxtech.heartbeat.server.HeartBeatServer;
import com.wxtech.heartbeat.util.RedisUtil;

public final class Context {	
	private static Logger logger = Logger.getLogger(Context.class);
    private Context() {
    }

    private static Config config;

    public static Config getConfig() {
        return config;
    }

    private static ObjectMapper objectMapper;

    public static ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    private static DataManager dataManager;

    public static DataManager getDataManager() {
        return dataManager;
    }
    
    private static HeartBeatServer serverManager;
    public static HeartBeatServer getServerManager() {
    	return serverManager;
    }

    public static void init(String path) throws Exception {

        config = new Config();
        config.load(path);

        objectMapper = new ObjectMapper();
        objectMapper.setConfig(
                objectMapper.getSerializationConfig().without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS));

        if (config.hasKey("database.url")) {
            dataManager = new DataManager(config);
        } else {
        	throw new RuntimeException("'database.url' is not set in config.xml");
        }
        
        if (config.hasKey("server.port")) {
        	serverManager = new HeartBeatServer(config);	
        } else {
        	throw new RuntimeException("'server.port' is not set in config.xml");
        }
        logger.debug("init redis pool...");
        RedisUtil.getInstance();
    }
}
